package com.example.lanouhn.firstapp.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.model.CollectionStore;
import com.example.lanouhn.firstapp.activity.model.Syomb;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;
import com.example.lanouhn.firstapp.activity.utils.SyombJson;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 我的收藏商品数据适配器
 * <p/>
 *
 */
public class CollectionStoreAdapter extends RecyclerView.Adapter {

    private String key;

    private Syomb os;
    //上下文对象
    private Context context;
    //数据集合
    private List<CollectionStore> list;


    //定义监听接口
    public interface CollectionStoreRecyclerViewListener{

        void onClick(View view, int position);
    }

    private CollectionStoreRecyclerViewListener collectionStoreRecyclerViewListener;

    public void setCollectionStoreRecyclerViewListener(CollectionStoreRecyclerViewListener collectionStoreRecyclerViewListener) {
        this.collectionStoreRecyclerViewListener = collectionStoreRecyclerViewListener;
    }



    public CollectionStoreAdapter(List<CollectionStore> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局
        RecyclerView.ViewHolder holder = null;
        holder = new VHolder(
                LayoutInflater.from(context).
                        inflate(R.layout.item_collection_store,null));

        //获取本地存储的key值
        key = (String) SPUtils.get(context,"log_key","");
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final VHolder vHolder = (VHolder) holder;
        //数据位置
        final CollectionStore collectionStore = list.get(position);
        //设置数据  运用picasso获取图片
        Picasso.with(context).load(collectionStore.getStore_avatar_url()).into(vHolder.image);
        vHolder.flagship_store_name.setText(collectionStore.getStore_name());
        vHolder.goods_count.setText(collectionStore.getGoods_count());
        vHolder.store_collect.setText(collectionStore.getStore_collect());
        vHolder.collection_time.setText(collectionStore.getFav_time_text());


        //recyclerView的item点击事件
        vHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collectionStoreRecyclerViewListener.onClick(vHolder.itemView,position);

            }
        });
        //删除收藏的商品点击事件
        vHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String url = "http://www.niaolema.com/mobile/index.php?act=member_favorites_store&op=favorites_del";
                        String params ="store_id=" + collectionStore.getStore_id() + "&key=" + key;
                        String result = HttpUtils.doPost(url,params);
                        if(!result.equals("") && !result.equals("null")){
                            os = SyombJson.getSyomb(result);

                            if(os.getDatas() == 1){
                                //解析成功
                                handler.sendEmptyMessage(1);
                            }else {
                                handler.sendEmptyMessage(0);
                            }

                        }else {
                            handler.sendEmptyMessage(0);

                        }
                    }
                }).start();


            }
            private Handler handler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    switch(msg.what){

                        case 0:
                            Toast.makeText(context, "请求失败,请稍候再试!!!", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:

                            Intent intent = new Intent();
                            intent.setAction("delete");
                            context.sendBroadcast(intent);

                            break;


                    }
                }
            };
        });



    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    //定义VHolder类继承RecyclerView.ViewHolder
    class VHolder extends RecyclerView.ViewHolder{

        //店铺名
        private TextView flagship_store_name;
        //商品数
        private TextView goods_count;
        //收藏时间
        private TextView collection_time;
        //收藏次数
        private TextView store_collect;
        //店铺标识
        private ImageView image;
        //删除
        private ImageView delete;

        //绑定控件id
        public VHolder(View itemView) {
            super(itemView);
            collection_time = (TextView) itemView.findViewById(R.id.collection_time);
            flagship_store_name = (TextView) itemView.findViewById(R.id.flagship_store_name);
            goods_count = (TextView) itemView.findViewById(R.id.goods_count);
            store_collect = (TextView) itemView.findViewById(R.id.collection_count);
            image = (ImageView) itemView.findViewById(R.id.image);
            delete = (ImageView) itemView.findViewById(R.id.delete);
        }
    }


}
